package com.prs.crm.action.crm;

import java.io.File;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.CustomerAgent;
import com.prs.crm.domain.crm.CustomerProject;
import com.prs.crm.domain.crm.CustomerSupplier;
import com.prs.crm.domain.crm.DocumenTary;
import com.prs.crm.domain.crm.Linkman;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.crm.DocumenTaryService;

public class DocumenTaryAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private Customer customer;
	private CustomerAgent customerAgent;
	private CustomerProject customerProject;
	private Linkman linkman;
	private CustomerSupplier customerSupplier;
	private DocumenTary documenTary;
	private DocumenTaryService documenTaryService;
	private PaginationSupport pagination;
	private Integer[] documenTaryIds;
	private File upload;
	private String uplodS;
	private String uploadFileName;
	
	@Action(value = "saveDocumenTary", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerOA"),
			@Result(name = "error", type = "chain", location = "editDocumenTary") })
	public String saveTary(){
		if(upload!=null){
			documenTary.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		this.documenTary.setCustomer(customer);
		this.documenTaryService.save(documenTary);
		return SUCCESS;
	}
	@Action(value = "saveDoc", results = {
			@Result(name = "success", type = "redirect", location = "viewCustomer?customer=${customer.getId()}"),
			@Result(name = "error", type = "chain", location = "editDocumenTary") })
	public String saveDoc(){
		this.documenTary.setCustomer(customer);
		this.documenTaryService.save(documenTary);
		return SUCCESS;
	}
	
	@Action("listCustomerDocumenTary")
	public String listCustomerDocumenTary() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DocumenTary.class);
		criteria.add(Restrictions.isNotNull("customer"));
		criteria.add(Restrictions.or(Restrictions.like("shareperson", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%"), Restrictions.like("taryperson", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%")));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDocumenTaryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listAgentDocumenTary")
	public String listAgentDocumenTary() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DocumenTary.class);
		criteria.add(Restrictions.isNotNull("customerAgent"));
		criteria.add(Restrictions.or(Restrictions.like("shareperson", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%"), Restrictions.like("taryperson","%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%")));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDocumenTaryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listProjectDocumenTary")
	public String listProjectDocumenTary() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DocumenTary.class);
		criteria.add(Restrictions.isNotNull("customerProject"));
		criteria.add(Restrictions.or(Restrictions.like("shareperson", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%"), Restrictions.like("taryperson", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%")));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDocumenTaryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listLinkmanDocumenTary")
	public String listLinkmanDocumenTary() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DocumenTary.class);
		criteria.add(Restrictions.isNotNull("linkman1"));
		criteria.add(Restrictions.or(Restrictions.like("shareperson", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%"), Restrictions.like("taryperson", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%")));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDocumenTaryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listSupplierDocumenTary")
	public String listSupplierDocumenTary() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				DocumenTary.class);
		criteria.add(Restrictions.isNotNull("customerSupplier"));
		criteria.add(Restrictions.or(Restrictions.like("shareperson", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%"), Restrictions.like("taryperson", "%"+AppContext.getAccountContext().currentUser().getResume().getName()+"%")));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDocumenTaryService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("viewDocumenTary")
	public String viewDocumenTary() {
		this.documenTary=this.documenTaryService.get(documenTaryIds[0]);
		this.customer=this.documenTary.getCustomer();
		return SUCCESS;
	}
	@Action("viewAgentDocumenTary")
	public String viewAgentDocumenTary() {
		this.documenTary=this.documenTaryService.get(documenTaryIds[0]);
		this.customerAgent=this.documenTary.getCustomerAgent();
		return SUCCESS;
	}
	@Action("viewProjectDocumenTary")
	public String viewProjectDocumenTary() {
		this.documenTary=this.documenTaryService.get(documenTaryIds[0]);
		this.customerProject=this.documenTary.getCustomerProject();
		return SUCCESS;
	}
	@Action("viewLinkmanDocumenTary")
	public String viewLinkmanDocumenTary() {
		this.documenTary=this.documenTaryService.get(documenTaryIds[0]);
		this.linkman=this.documenTary.getLinkman1();
		return SUCCESS;
	}
	@Action("viewSupplierDocumenTary")
	public String viewSupplierDocumenTary() {
		this.documenTary=this.documenTaryService.get(documenTaryIds[0]);
		this.customerSupplier=this.documenTary.getCustomerSupplier();
		return SUCCESS;
	}
	
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public DocumenTary getDocumenTary() {
		return documenTary;
	}
	public void setDocumenTary(DocumenTary documenTary) {
		this.documenTary = documenTary;
	}

	public DocumenTaryService getDocumenTaryService() {
		return documenTaryService;
	}

	public void setDocumenTaryService(DocumenTaryService documenTaryService) {
		this.documenTaryService = documenTaryService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getDocumenTaryIds() {
		return documenTaryIds;
	}

	public void setDocumenTaryIds(Integer[] documenTaryIds) {
		this.documenTaryIds = documenTaryIds;
	}

	public CustomerAgent getCustomerAgent() {
		return customerAgent;
	}

	public void setCustomerAgent(CustomerAgent customerAgent) {
		this.customerAgent = customerAgent;
	}

	public CustomerProject getCustomerProject() {
		return customerProject;
	}

	public void setCustomerProject(CustomerProject customerProject) {
		this.customerProject = customerProject;
	}

	public Linkman getLinkman() {
		return linkman;
	}

	public void setLinkman(Linkman linkman) {
		this.linkman = linkman;
	}

	public CustomerSupplier getCustomerSupplier() {
		return customerSupplier;
	}

	public void setCustomerSupplier(CustomerSupplier customerSupplier) {
		this.customerSupplier = customerSupplier;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUplodS() {
		return uplodS;
	}
	public void setUplodS(String uplodS) {
		this.uplodS = uplodS;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
}
